package com.seu.vcampus.handler;

import com.seu.vcampus.message.Message;
import com.seu.vcampus.service.ShopService;
import com.seu.vcampus.vo.CartItem;
import com.seu.vcampus.vo.Order;
import com.seu.vcampus.vo.Product;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * 商店消息处理器
 * 处理商店相关的消息请求
 * 
 * @author 虚拟校园开发团队
 * @since 1.0.0
 */
public class ShopHandler {
	private final ShopService shopService;
	
	public ShopHandler(Connection connection)
	{
		this.shopService = new ShopService(connection);
	}
	
	/**
	 * 处理消息
	 */
	public Message handle(Message request)
	{
		try
		{
			Message.Type type = request.getType();
			if(type == Message.Type.GET_PRODUCTS_REQUEST)
			{
				return handleGetAllProducts(request);
			}
			else if(type == Message.Type.GET_PRODUCT_BY_ID_REQUEST)
			{
				return handleGetProductById(request);
			}
			else if(type == Message.Type.GET_PRODUCT_BY_NAME_REQUEST)
			{
				return handleGetProductByName(request);
			}
			else if(type == Message.Type.SEARCH_PRODUCT_REQUEST)
			{
				return handleSearchProduct(request);
			}
			else if(type == Message.Type.SAVE_PRODUCT_REQUEST)
			{
				return handleSaveProduct(request);
			}
			else if(type == Message.Type.DELETE_PRODUCT_REQUEST)
			{
				return handleDeleteProduct(request);
			}
			else if(type == Message.Type.GET_PRODUCT_COUNT_REQUEST)
			{
				return handleGetProductCount(request);
			}
			else if(type == Message.Type.UPDATE_PRODUCT_STOCK_REQUEST)
			{
				return handleUpdateProductStock(request);
			}
			else if(type == Message.Type.GET_ORDER_BY_ID_REQUEST)
			{
				return handleGetOrderById(request);
			}
			else if(type == Message.Type.GET_USER_ORDERS_REQUEST)
			{
				return handleGetUserOrders(request);
			}
			else if(type == Message.Type.GET_ALL_ORDERS_REQUEST)
			{
				return handleGetAllOrders(request);
			}
			else if(type == Message.Type.DELETE_SINGLE_ORDER_REQUEST)
			{
				return handleDeleteOrder(request);
			}
			else if(type == Message.Type.DELETE_USER_ORDERS_REQUEST)
			{
				return handleDeleteUserOrders(request);
			}
			else if(type == Message.Type.GET_USER_ORDER_COUNT_REQUEST)
			{
				return handleGetUserOrderCount(request);
			}
			else if(type == Message.Type.GET_USER_CART_REQUEST)
			{
				return handleGetUserCart(request);
			}
			else if(type == Message.Type.UPDATE_USER_CARTITEM_REQUEST)
			{
				return handleUpdateUserCartItem(request);
			}
			else if(type == Message.Type.BUY_PRODUCT_REQUEST)
			{
				return handleUserBuy(request);
			}
			else if(type == Message.Type.ADD_TO_CART_REQUEST)
			{
				return handleAddToCart(request);
			}
			else
			{
				return Message.error("未知的消息类型");
			}
		}
		catch(Exception e)
		{
			return Message.error("处理商店消息时发生错误：" + e.getMessage());
		}
	}
	
	/**
	 * 处理获取所有商品的请求
	 */
	public Message handleGetAllProducts(Message request)
	{
		List<Product> products = shopService.getAllProducts();
		return Message.success(Message.Type.GET_PRODUCTS_RESPONSE, products);
	}
	
	/**
	 * 处理根据ID获取商品的请求
	 */
	public Message handleGetProductById(Message request)
	{
		String pid = (String) request.getPayload();
		Product product = shopService.getProductById(pid);
		return Message.success(Message.Type.GET_PRODUCT_BY_ID_RESPONSE, product);
	}
	
	/**
	 * 处理根据名字获取商品的请求
	 */
	public Message handleGetProductByName(Message request)
	{
		String name = (String) request.getPayload();
		Product product = shopService.getProductByName(name);
		return Message.success(Message.Type.GET_PRODUCT_BY_NAME_RESPONSE, product);
	}
	
	/**
	 * 处理关键词搜索商品的请求
	 */
	public Message handleSearchProduct(Message request)
	{
		String keywords = (String) request.getPayload();
		List<Product> products = shopService.searchProduct(keywords);
		return Message.success(Message.Type.SEARCH_PRODUCT_RESPONSE, products);
	}
	
	/**
	 * 处理存储商品的请求
	 */
	public Message handleSaveProduct(Message request)
	{
		Product product = (Product) request.getPayload();
		boolean success = shopService.saveProduct(product);
		return Message.success(Message.Type.SAVE_PRODUCT_RESPONSE, success);
	}
	
	/**
	 * 处理删除商品的请求
	 */
	public Message handleDeleteProduct(Message request)
	{
		String pid = (String) request.getPayload();
		boolean success = shopService.deleteProduct(pid);
		return Message.success(Message.Type.DELETE_PRODUCT_RESPONSE, success);
	}
	
	/**
	 * 处理获取商品数量的请求
	 */
	public Message handleGetProductCount(Message request)
	{
		int count = shopService.getProductCount();
		return Message.success(Message.Type.GET_PRODUCT_COUNT_RESPONSE, count);
	}
	
	/**
	 * 处理更新商品库存的请求
	 */
	public Message handleUpdateProductStock(Message request)
	{
		Map<String, Object> map = (Map<String, Object>) request.getPayload();
		String pid = (String) map.get("pid");
		int stock = (int) map.get("stock");
		boolean success = shopService.updateProductStock(pid, stock);
		return Message.success(Message.Type.UPDATE_PRODUCT_STOCK_RESPONSE, success);
	}
	
	/**
	 * 处理根据订单号查找订单的请求
	 */
	public Message handleGetOrderById(Message request)
	{
		String oid = (String) request.getPayload();
		Order order = shopService.getOrderById(oid);
		return Message.success(Message.Type.GET_ORDER_BY_ID_RESPONSE, order);
	}
	
	/**
	 * 处理获取用户订单的请求
	 */
	public Message handleGetUserOrders(Message request)
	{
		String uid = (String) request.getPayload();
		List<Order> orders = shopService.getUserOrders(uid);
		return Message.success(Message.Type.GET_USER_ORDERS_RESPONSE, orders);
	}
	
	/**
	 * 处理获取所有订单的请求
	 */
	public Message handleGetAllOrders(Message request)
	{
		List<Order> orders = shopService.getAllOrders();
		return Message.success(Message.Type.GET_ALL_ORDERS_RESPONSE, orders);
	}
	
	/**
	 * 处理删除订单的请求
	 */
	public Message handleDeleteOrder(Message request)
	{
		String oid = (String) request.getPayload();
		boolean success = shopService.deleteOrder(oid);
		return Message.success(Message.Type.DELETE_SINGLE_ORDER_RESPONSE, success);
	}
	
	/**
	 * 处理删除用户订单的请求
	 */
	public Message handleDeleteUserOrders(Message request)
	{
		String uid = (String) request.getPayload();
		boolean success = shopService.deleteUserOrders(uid);
		return Message.success(Message.Type.DELETE_USER_ORDERS_RESPONSE, success);
	}
	
	/**
	 * 处理获取用户订单数量的请求
	 */
	public Message handleGetUserOrderCount(Message request)
	{
		String uid = (String) request.getPayload();
		int count = shopService.getUserOrderCount(uid);
		return Message.success(Message.Type.GET_USER_ORDER_COUNT_RESPONSE, count);
	}
	
	/**
	 * 处理获取用户购物车的请求
	 */
	public Message handleGetUserCart(Message request)
	{
		String uid = (String) request.getPayload();
		List<CartItem> cart = shopService.getUserCart(uid);
		return Message.success(Message.Type.GET_USER_CART_RESPONSE, cart);
	}
	
	/**
	 * 处理更新用户购物车内容的请求
	 */
	public Message handleUpdateUserCartItem(Message request)
	{
		CartItem cartItem = (CartItem) request.getPayload();
		boolean success = shopService.updateUserCartItem(cartItem);
		return Message.success(Message.Type.UPDATE_USER_CARTITEM_RESPONSE, success);
	}
	
	/**
	 * 处理用户购买商品的请求
	 */
	public Message handleUserBuy(Message request)
	{
		Map<String, Object> map = (Map<String, Object>) request.getPayload();
		String uid = (String) map.get("uid");
		String pid = (String) map.get("pid");
		int quantity = (int) map.get("quantity");
		boolean success = shopService.userBuy(uid, pid, quantity);
		return Message.success(Message.Type.BUY_PRODUCT_RESPONSE, success);
	}
	
	/**
	 * 处理将商品添加到购物车的请求
	 */
	public Message handleAddToCart(Message request)
	{
		Map<String, Object> map = (Map<String, Object>) request.getPayload();
		String uid = (String) map.get("uid");
		String pid = (String) map.get("pid");
		int quantity = (int) map.get("quantity");
		boolean success = shopService.addProductToCart(uid, pid, quantity);
		return Message.success(Message.Type.ADD_TO_CART_RESPONSE, success);
		
	}
	
}
